package com.example.test.conview.view.core;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;

import com.google.zxing.BarcodeFormat;
import com.puty.conputyutils.LogUtils;
import com.puty.conputyutils.StringUtils;
import com.puty.conputyutils.common.PutyUtilsConst;
import com.puty.conputyview.common.PutyConst;
import com.puty.conputyview.view.tool.DimensionCodeUtils;
import com.puty.conputyview.view.tool.Global;

/**
 * 一维码处理
 */
public class BarCodeOneElement extends Element {
    private final static String TAG = "BarCodeOneElement";
    public Bitmap img; // Bitmap图片
    public Bitmap tempImg; // 临时 Bitmap

    //打开方式
    public BarcodeFormat barcodeFormat;

    public BarCodeOneElement(Context context, String content, float width, float height,
                             int bformat, Label lb) {
        super(context, lb);
        super.type = PutyConst.BAR_CODE;
        this.content = content;
        this.width = width * lb.scalingRatio;
        this.height = height * lb.scalingRatio;
        this.textMode = 1; //居中对齐
        this.fontIndex = getFontIndex(PutyConst.SERIES_TYPE == PutyConst.INDUSTRY_SERIES ? 2 : 8, lb.Width, lb.Height);
        //生成39码
        if (bformat == 2) {
            super.bformat = bformat;
        }else if (PutyConst.SERIES_TYPE == PutyConst.INDUSTRY_SERIES && bformat==0) {
            super.bformat = 0;
        }
       if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
            super.orientation = lb.printInfo.PrintDirect; //打印方向 默认水平
        }
    }

    /*
     * 设置字体大小
     *
     * */
    @Override
    public void setFontSize() {
        //字体大小
        this.fontSize = Global.fontSize[Math.abs(fontIndex)] * 8 * lb.scale / compressionRatio;
        LogUtils.i(TAG,"fontSize:"+this.fontSize);
    }

    @Override
    public void init() {
        super.init();
        setFontSize();
        LogUtils.i(TAG,"init ...");

        /**
         * 一维码 文字
         * */
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        /**
         * 抗锯齿
         * */
        textPaint.setAntiAlias(true);
        textPaint.setTypeface(this.typeface);
        LogUtils.i(TAG,"set fontSize:"+this.fontSize);
        textPaint.setTextSize(this.fontSize);
        textPaint.setColor(Color.BLACK);
        /**
         * 加粗
         * */
        textPaint.setFakeBoldText(fontBlod == 0 ? false : true);
        textPaint.setTextSkewX(fontItalic == 0 ? 0 : -0.25f);
        textPaint.setUnderlineText(fontUnderline == 0 ? false : true);
        textPaint.setStrikeThruText(fontDelete == 0 ? false : true);//删除线
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textHeight = fontMetrics.bottom - fontMetrics.top;//文字的高度
        float textWidth = getContentWhidth(textPaint,"5");//文字的宽度
        if (this.textPlace == 0) {
            textHeight = 0;
        }

        if (TextUtils.isEmpty(content)) {
            content = "12345678";
        }
        String message = content;
        LogUtils.i(TAG,"bformat:"+bformat+"_content:"+content);
        if (PutyConst.SERIES_TYPE == PutyConst.INDUSTRY_SERIES && bformat > 7) {
            barcodeFormat = BarcodeFormat.CODE_128;
            //检验内容是否符合规范，不规范则进行修改
            message = StringUtils.changecontent(mContext, PutyUtilsConst.BFORMAT_128_CODE, content);
        }
        //检验内容是否符合规范，不规范则进行修改
        message = StringUtils.changecontent(mContext, bformat, content);
        switch (bformat) {
            case PutyUtilsConst.BFORMAT_DEFAULT_CODE:
                barcodeFormat = BarcodeFormat.CODE_128;
                break;
            case PutyUtilsConst.BFORMAT_TIF25_CODE:
                barcodeFormat = BarcodeFormat.ITF;
                break;
            case PutyUtilsConst.BFORMAT_39_CODE:
                barcodeFormat = BarcodeFormat.CODE_39;
                break;
            case PutyUtilsConst.BFORMAT_128_CODE:
                barcodeFormat = BarcodeFormat.CODE_128;
                break;
            case PutyUtilsConst.BFORMAT_KUDBAR_CODE:
                barcodeFormat = BarcodeFormat.CODABAR;
                break;
            case PutyUtilsConst.BFORMAT_EAN_8_CODE:
                barcodeFormat = BarcodeFormat.EAN_8;
                break;
            case PutyUtilsConst.BFORMAT_EAN_13_CODE:
                barcodeFormat = BarcodeFormat.EAN_13;
                break;
            case PutyUtilsConst.BFORMAT_UPC_CODE:
                barcodeFormat = BarcodeFormat.UPC_A;
                break;
            default:
                break;
        }
        int bmpWidth = 1;
        int mergin = 0;
        Bitmap.Config config = Bitmap.Config.ARGB_4444;
        // 家用的特别处理
        if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
            bmpWidth = 200;
            mergin = 3;
            config = Bitmap.Config.ARGB_8888;
            // 1、计算宽度，每位数对应2mm，不小于8位数长度
            int contentLength = content.length() >= 8 ? content.length() : 8;
            if (orientation == 0) {
                this.width = contentLength * 3.5f * scale * 8; // 宽度
                this.rate = 0;
            } else {
                this.height = contentLength * 3.5f * scale * 8; // 宽度
                this.rate = 90;
            }
        }
        float width = this.width / compressionRatio;
        float height = this.height / compressionRatio;
        if (this.rate == PutyConst.RATE_90 || this.rate == PutyConst.RATE_270) {
            float temp;
            temp = width;
            width = height;
            height = temp;
        }

        Bitmap bmp = DimensionCodeUtils
                .createOneCodeImage(message, bmpWidth, 1, barcodeFormat,mergin);
        if (bmp == null || width == 0 || height == 0) {
            return;
        }
        LogUtils.i("TAG", "BitmapW = " + bmp.getWidth() + ",   BitmapH = " + bmp
                .getHeight() + " ,  width = " + width + ",   height = " + height);
        Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, config);
        if (bitmap != null) {
            this.tempImg = bitmap;
        }
        Canvas canvas = new Canvas(this.tempImg);
        float left = 0;
        float bottom = 0;
        float top = 0;
        float rectLeft = left;
        float rectRight = width;
        if (bformat == 6) {
            rectLeft = left + textWidth;
        } else if (bformat == 7) {
            rectLeft = left + textWidth;
            rectRight = width - textWidth;
        }
        bottom = height - textHeight;
        //画条码
        switch (this.textPlace) {
            case PutyUtilsConst.BAR_CODE_ON_TOP://条码上方
                canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getHeight()),
                        new RectF(rectLeft, textHeight, rectRight, height), null);
                break;
            case PutyUtilsConst.BAR_CODE_BELOW://条码下方
                canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getHeight()),
                        new RectF(rectLeft, 0, rectRight, bottom), null);
                break;
            default:
                canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getHeight()),
                        new RectF(0, 0, width, height), null);
                break;
        }
        //画文本
        if (textPlace > 0) {
            switch (textMode) {
                case PutyUtilsConst.ALIGNMENT_MODE_ON_CENTER://居中
                    if (PutyUtilsConst.BFORMAT_EAN_13_CODE == bformat) {
                        if (PutyUtilsConst.BAR_CODE_BELOW == textPlace) {
                            textalignEan_13Center(left, top, textHeight, message, textPaint, canvas);
                        } else {
                            textalignEan_13CenterXia(left, top, textHeight, message, textPaint, canvas);
                        }
                    } else if (PutyUtilsConst.BFORMAT_EAN_8_CODE == bformat) {
                        if (PutyUtilsConst.BAR_CODE_BELOW == textPlace) {
                            textalignEan_8Center(left, top, textHeight, message, textPaint, canvas);
                        } else {
                            textalignEan_8CenterXia(left, top, textHeight, message, textPaint, canvas);
                        }
                    } else if (PutyUtilsConst.BFORMAT_UPC_CODE == bformat) {
                        if (PutyUtilsConst.BAR_CODE_BELOW == textPlace) {
                            textalignUpc_ACenter(left, top, textHeight, message, textPaint, canvas);
                        } else {
                            textalignUpc_ACenterXia(left, top, textHeight, message, textPaint, canvas);
                        }
                    } else {
                        textalignCenter(left, top, textHeight, message, textPaint, canvas);
                    }
                    break;
                case PutyUtilsConst.ALIGNMENT_MODE_ON_RIGHT://居右
                    if (PutyUtilsConst.BFORMAT_EAN_13_CODE == bformat) {
                        textalignEan_13Center(left, top, textHeight, message, textPaint, canvas);
                    } else if (PutyUtilsConst.BFORMAT_EAN_8_CODE == bformat) {
                        textalignEan_8Center(left, top, textHeight, message, textPaint, canvas);
                    } else if (PutyUtilsConst.BFORMAT_UPC_CODE == bformat) {
                        if (PutyUtilsConst.BAR_CODE_BELOW == textPlace) {
                            textalignUpc_ACenter(left, top, textHeight, message, textPaint, canvas);
                        } else {
                            textalignUpc_ACenterXia(left, top, textHeight, message, textPaint, canvas);
                        }
                    } else {
                        textalignRight(left, top, textHeight, message, textPaint, canvas);
                    }
                    break;
                case PutyUtilsConst.ALIGNMENT_MODE_STRETCH://拉伸
                    if (PutyUtilsConst.BFORMAT_EAN_13_CODE == bformat) {
                        textalignEan_13Center(left, top, textHeight, message, textPaint, canvas);
                    } else if (PutyUtilsConst.BFORMAT_EAN_8_CODE == bformat) {
                        textalignEan_8Center(left, top, textHeight, message, textPaint, canvas);
                    } else if (PutyUtilsConst.BFORMAT_UPC_CODE == bformat) {
                        if (PutyUtilsConst.BAR_CODE_BELOW == textPlace) {
                            textalignUpc_ACenter(left, top, textHeight, message, textPaint, canvas);
                        } else {
                            textalignUpc_ACenterXia(left, top, textHeight, message, textPaint, canvas);
                        }
                    } else {
                        int padding =  this.fontItalic == 1 ? 8 : 0;
                        int len =  message.length();
                        //获取总的长度
                        float total = getContentWhidth(textPaint,message);

                        float surplusWidth = width - total - textCellSpace * (len - 1) - padding;
                        float space = surplusWidth / (len - 1);//获取字符空隙 总宽度-字符串宽度

                        textalignPP(left, top, textHeight, message, textPaint, canvas, space);
                    }
                    break;
                default:
                    if (PutyUtilsConst.BFORMAT_EAN_13_CODE == bformat) {
                        textalignEan_13Center(left, top, textHeight, message, textPaint, canvas);
                    } else if (PutyUtilsConst.BFORMAT_EAN_8_CODE == bformat) {
                        textalignEan_8Center(left, top, textHeight, message, textPaint, canvas);
                    } else if (PutyUtilsConst.BFORMAT_UPC_CODE == bformat) {
                        if (PutyUtilsConst.BAR_CODE_BELOW == textPlace) {
                            textalignUpc_ACenter(left, top, textHeight, message, textPaint, canvas);
                        } else {
                            textalignUpc_ACenterXia(left, top, textHeight, message, textPaint, canvas);
                        }

                    } else {
                        textalignLeft(left, top, textHeight, message, textPaint, canvas);
                    }
                    break;
            }
        }
        this.rate(this.rate);
    }

    /**
     * 画条码文本
     *
     * @param degree 旋转角度
     */
    @Override
    public void rate(int degree) {
        this.img = this.tempImg;
        Matrix matrix = new Matrix();
        matrix.setRotate(degree, this.img.getWidth() / 2, this.img.getHeight() / 2);
        this.img = Bitmap.createBitmap(this.img, 0, 0, this.img.getWidth(), this.img.getHeight(), matrix, true);
        rotation(degree);
    }

    @Override
    public void zoom(float x, float y, float width, float height) {
        if (isLock == 1) {
            return;
        }
        super.zoom(x, y, width, height);
        //往X方向
        if (this.zoomdirect == 0) {
            this.width += x;
            if (PutyConst.RATE_BOUNDARY_100 >= this.width) {
                this.width = PutyConst.RATE_BOUNDARY_100;
            } else if (this.left + this.width + PutyConst.RATE_INCREASE >= width) {
                this.width -= x;
                return;
            }
        } else {
            this.height += y;
            if (PutyConst.RATE_BOUNDARY  >= this.width) {
                this.height = PutyConst.RATE_BOUNDARY;
            } else if (this.top + this.height + PutyConst.RATE_INCREASE >= height) {
                this.height -= y;
                return;
            }
        }
        LogUtils.i(TAG, "一维码222222222");
        this.init();
        zoomOtherSelected(x, y);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (img == null) {
            return;
        }
        // 家用系列特别处理
        if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES && orientation != 0) {
            rate(90);
        }
        //划线
        Rect sr = new Rect(0, 0, this.img.getWidth(), this.img.getHeight());
        canvas.drawBitmap(this.img, sr, new RectF(left, top,
                left + width, top + height), null);
        if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
            return;
        }
        // 工业系列特别处理
        if (isselected && isLock == 0) {
            //画缩放框
            //画右边缩放图标
            float hl = this.left + width - this.puhBitmap.getWidth() / 2;
            float ht = this.top + height / 2 - this.puhBitmap.getHeight() / 2;
            float hr = this.left + width + this.puhBitmap.getWidth() / 2;
            float hb = this.top + height / 2 + this.puhBitmap.getHeight() / 2;
            Rect rs = new Rect(0, 0, this.puhBitmap.getWidth(), this.puhBitmap.getHeight());
            RectF ds = new RectF(hl, ht, hr, hb);
            canvas.drawBitmap(this.puhBitmap, rs, ds, null);

            //画底部缩放图标
            hl = this.left + width / 2 - this.puvBitmap.getWidth() / 2;
            ht = this.top + height - this.puvBitmap.getHeight() / 2;
            hr = this.left + width / 2 + this.puvBitmap.getWidth() / 2;
            hb = this.top + height + this.puvBitmap.getHeight() / 2;
            rs = new Rect(0, 0, this.puvBitmap.getWidth(), this.puvBitmap.getHeight());
            ds = new RectF(hl, ht, hr, hb);
            canvas.drawBitmap(this.puvBitmap, rs, ds, null);

            //画删除按钮
            hl = this.left - this.deleteBitmap.getWidth() / 2;
            ht = this.top - this.deleteBitmap.getHeight() / 2;
            hr = this.left + this.deleteBitmap.getWidth() / 2;
            hb = this.top + this.deleteBitmap.getHeight() / 2;
            rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
            ds = new RectF(hl, ht, hr, hb);
            canvas.drawBitmap(this.deleteBitmap, rs, ds, null);
        }
    }

    @Override
    public BaseElement isscale(float x, float y) {
        if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
            return super.isscale(x,y);
        }
        //文本
        RectF rectF;
        float hl, ht, hr, hb;

        hl = this.left + this.width - scaleImageWidth / 2;
        ht = this.top + this.height / 2 - scaleImageWidth / 2;
        hr = this.left + this.width + scaleImageWidth / 2;
        hb = this.top + this.height / 2 + scaleImageWidth / 2;

        rectF = new RectF(hl, ht, hr, hb);
        if ((x >= rectF.left && x <= rectF.right) && (y >= rectF.top && y <= rectF.bottom)) {
            if(lb.isMunSelect == 1){//多选
                for (BaseElement e:lb.Elements) {
                    if(e.isselected){
                        e.zoomdirect = 0;
                    }
                }
            }else {
                this.zoomdirect = 0;//X方向
            }
            return this;
        }

        hl = this.left + this.width / 2 - scaleImageWidth / 2;
        ht = this.top + this.height - scaleImageWidth / 2;
        hr = this.left + this.width / 2 + scaleImageWidth / 2;
        hb = this.top + this.height + scaleImageWidth / 2;

        rectF = new RectF(hl, ht, hr, hb);
        if ((x >= rectF.left && x <= rectF.right) && (y >= rectF.top && y <= rectF.bottom)) {
            if(lb.isMunSelect == 1){//多选
                for (BaseElement e:lb.Elements) {
                    if(e.isselected){
                        e.zoomdirect = 1;
                    }
                }
            }else {
                this.zoomdirect = 1;//Y方向
            }
            return this;
        }
        return null;
    }

    /**
     * 复制对象
     *
     * @param element 原始对象
     * @param barCodeOneElement 新的对象
     * @return 新的对象
     */
    public BarCodeOneElement clone(Element element, BarCodeOneElement barCodeOneElement) {
        BarCodeOneElement barcodeelement = (BarCodeOneElement) element;
        barCodeOneElement.scale = barcodeelement.scale; // 缩放比例
        barCodeOneElement.top = barcodeelement.top; // 元素顶部距离
        barCodeOneElement.left = barcodeelement.left; // 元素左侧距离
        barCodeOneElement.bformat = barcodeelement.bformat; // 编码规范
        barCodeOneElement.fontDelete = barcodeelement.fontDelete; // 删除线
        barCodeOneElement.dataSourceColIndex = barcodeelement.dataSourceColIndex; // 数据源的列号
        barCodeOneElement.inputMode = barcodeelement.inputMode; // 内容输入模式 0 手工 1 excel数据
        barCodeOneElement.dataSourceColName = barcodeelement.dataSourceColName; // 数据源名称
        barCodeOneElement.textPlace = barcodeelement.textPlace; // 条码位置
        barCodeOneElement.fontBlod = barcodeelement.fontBlod; // 字体粗细 一维码 。文本
        barCodeOneElement.dataSourceRowIndex = barcodeelement.dataSourceRowIndex; // 数据行号
        barCodeOneElement.familyName = barcodeelement.familyName; // 字体名称
        barCodeOneElement.barcodeFormat = barcodeelement.barcodeFormat; // 编码类型
        barCodeOneElement.textMode = barcodeelement.textMode; // 对齐模式
        barCodeOneElement.ddStep = barcodeelement.ddStep; // 递变数据 增长步长
        barCodeOneElement.fontIndex = barcodeelement.fontIndex; // 字体大小
        barCodeOneElement.fontUnderline = barcodeelement.fontUnderline; // 下划线
        barCodeOneElement.typeface = barcodeelement.typeface; // 字体类型
        barCodeOneElement.rate = barcodeelement.rate; // 旋转角度
        barCodeOneElement.fontItalic = barcodeelement.fontItalic; // 斜体 一维码 。文本
        barCodeOneElement.isPrinter = barcodeelement.isPrinter; // 0 不是打印对象 1打印
        barCodeOneElement.textCellSpace = barcodeelement.textCellSpace; // 字间距
        // 家用系列
        if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
            barCodeOneElement.width = barcodeelement.width; // 元素宽
            barCodeOneElement.height = barcodeelement.height; // 元素高
        }
        // 工业系列
        if (PutyConst.SERIES_TYPE == PutyConst.INDUSTRY_SERIES) {
            barCodeOneElement.isMirror = barcodeelement.isMirror; // 镜像
            barCodeOneElement.type = barcodeelement.type; // 元素类型
            barCodeOneElement.isblack = barcodeelement.isblack; // 黑白显示
            barCodeOneElement.isShow = barcodeelement.isShow; // 是否显示
        }
        return barCodeOneElement;
    }
}
